

import 'package:baselib/utils/load_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:baselib/widgets/comm_message_gradient_dialog.dart';
import 'package:baselib/widgets/refresh_util.dart';
import 'package:flutter/material.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:framelib/config/api_config.dart';
import 'package:framelib/net/baseentity.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:get/state_manager.dart';
import 'package:get/get.dart';
import 'package:module_business/module_index/model/index_room_item_entity.dart';
import 'package:module_business/module_index/service/module_index_service.dart';
import 'package:module_business/module_my/service/module_my_service.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';


class MyFollowViewModel extends BaseViewModel<ModuleMyService>{

  ModuleMyService moduleMyService;
  ModuleIndexService moduleIndexService;
  RefreshController refreshController = RefreshController(initialRefresh: true);
  RefreshState loadStatus=RefreshState.OK;
  int page = 1;
  bool isCanMoreData = false;
  List<IndexRoomItemEntity> myCollectionRooms = [];

  @override
  ModuleMyService createService(){
    moduleIndexService = ModuleIndexService();
    return moduleMyService = ModuleMyService();
  }
  @override
  void onInit() {
    super.onInit();
    Map<String,dynamic> arguments = Get.arguments??{};
    // roomInfo =  arguments['roomInfo'];
  }

  @override
  void onClose() {
    super.onClose();
  }
  onRefresh({int pageCur,}) async{
    if(pageCur!=null)page=pageCur;
    Map<String,dynamic> upData={
      'currentPage': page,
      'pageSize': 10,
    };
    BaseEntity baseEntity= await moduleIndexService
        .api_voicechat_room_queryCollectionRoomList(parmsMap: upData);
    if(refreshController!=null)refreshController.refreshCompleted();
    if(refreshController!=null)refreshController.loadComplete();
    if(baseEntity?.code==ApiConfig.SUCCESS_CODE){
      List<IndexRoomItemEntity> dataTemp=baseEntity?.result??[];
      if(page==1){
        myCollectionRooms=dataTemp;
      }else{
        myCollectionRooms.addAll(dataTemp);
      }
      if(((dataTemp?.length??0)<10)){
        isCanMoreData=false;
      }else{
        isCanMoreData=true;
      }
      //计算是否为空
      if(((myCollectionRooms?.length??0)==0)){
        loadStatus=RefreshState.EMPTY;
      }else{
        //计算被选中的
        loadStatus=RefreshState.OK;
      }
    }else{
      loadStatus=RefreshState.NET_ERROR;
      ToastUtil.toast(baseEntity?.message);
    }
    update();
  }
  doCollectRoom(IndexRoomItemEntity myCollectionRoomItem,int index){
    showDialog(
        context: Get.context,
        builder: (_){
          return CommMessageGradientDialog(
            message: '${StringUtil.instance.Revoke} ${myCollectionRoomItem?.roomName} ${StringUtil.instance.from_the_collections}?',
            onPositivePressEvent: (){
              LogUtil.d('showDialog-->onPositivePressEvent');
              Navigator.of(Get.context).pop();
              startDoCollectRoom(myCollectionRoomItem,index);
            },
            onCloseEvent: (){
              LogUtil.d('showDialog-->onCloseEvent');
              Navigator.of(Get.context).pop();
            },
          );
        }
    );
  }

  void startDoCollectRoom(IndexRoomItemEntity myCollectionRoomItem, int index) async{
    LoadUtil.showLoad(loadingText: '${StringUtil.instance.submiting}...');
    Map<String,dynamic> upData={
      "searchValue": myCollectionRoomItem?.id
    };
    BaseEntity baseEntity = await moduleIndexService.api_voicechat_room_collectionOrUnRoom(
        parmsMap: upData,isCollection: false);
    LoadUtil.hideLoad();
    if(baseEntity?.code == ApiConfig.SUCCESS_CODE){
      myCollectionRooms.removeAt(index);
      update();
    }else{
      ToastUtil.toast(baseEntity?.message);
    }
  }

}
